var app = getApp(),
    fn = app.fn;

Page({
    data: {
        sortTab: {
            item: [
                { txt: '新品', type: 1, default: "" },
                { txt: '价格', type: 2, up: 2, down: 22 },
                { txt: '销量', type: 2, up: 11, down: 1 },
                { txt: '人气', type: 2, up: 44, down: 4 },
            ],
            curClass: {
                "cur": "cur",
                "up": "up",
                "down": "down"
            },
            cur: {
                num: 0,
                status: 'cur'
            }
        },
        proList: {
            data: [],
            loading: false,
            loadend: false,
            reqData: {
                name: '',
                sorting: '',
                pageNo: 0
            }
        }
    },
    onLoad: function (options) {
        var searchName = options.kw;
      
        this.setData({
            'proList.reqData.name': searchName
        });
   
        wx.setNavigationBarTitle({
            title: searchName
        })
    
        this.getMore();
    },
   
    onPullDownRefresh: function () {
        this.setData({
            'proList.data': [],
            'proList.loading': false,
            'proList.loadend': false,
            'proList.reqData.pageNo': 0,
        });
        this.getMore(function () {
            wx.stopPullDownRefresh();
        });
    },

    onReachBottom: function () {
        this.getMore();
    },

    sortChange: function (e) {
        var that = this,
            _num = e.currentTarget.dataset.num,
            curData = that.data.sortTab.item[_num];

        var _status = '', _sorting = '';
        switch (curData.type) {
            case 1:
                _status = 'cur';
                break;
            case 2:
                if (that.data.sortTab.cur.num == _num && that.data.sortTab.cur.status == 'up') {
                    _status = 'down';
                } else {
                    _status = 'up';
                }
                _sorting = curData[_status];
                break;
        }
        that.setData({
            'sortTab.cur.num': _num,
            'sortTab.cur.status': _status,
            'proList.reqData.sorting': _sorting,
            'proList.data': [],
            'proList.loading': false,
            'proList.loadend': false,
            'proList.reqData.pageNo': 0,
        });
        that.getMore();
    },
    getMore: function (callback) {
        var that = this,
            pageNo = ++that.data.proList.reqData.pageNo;

        if (that.data.proList.loadend || that.data.proList.loading) return false;

  
        that.setData({
            "loading": true,
            "proList.reqData.pageNo": pageNo
        });

    
        fn.apiData({
            url: 'searchAll.do',
            data: that.data.proList.reqData,
            success: function (res) {
                var newData = res.data.result.product;
                if (newData.length == 0 || !newData) {
                  
                    that.setData({
                        'proList.loadend': true
                    });
                } else {
                
                    newData = that.data.proList.data.concat(newData);
                    that.setData({
                        'proList.data': newData,
                        'proList.loading': false
                    });
                }

                if (!!callback) callback();

          
                wx.hideToast();
            }
        });
    }
})
